<?php

/**
 * System config class
 * @author Gmind Software House
 * @version 1.02
 * @updated 20-lip-2012 13:39:37
 */
class Rcp_Config {

  static private $_instance = null;
  protected $xmlConf = array('allowModifications' => true);
  private $config;
  private $appConfig;

  private function __construct() {
  }

  /**
   * Class constructor 
   */
  public function init($config) {
    $this->appConfig = Zend_Registry::get('appConfig');
    if (!file_exists($this->appConfig->app->config->paths)) {
      $this->createConfig();
    }

    $this->config = ($config != null) ? $config : new Zend_Config_Xml(Zend_Controller_Front::getInstance()->getModuleDirectory() . $this->appConfig->app->modules->config->paths, null, $this->xmlConf);
  }

  public static function getInstance($config=null) {
    if (self::$_instance === null) {
      self::$_instance = new self();
      self::$_instance->init($config);
    }
    return self::$_instance;
  }

  /**
   * getter 
   * 
   * @param string $property - name of config parameter
   * @return string - value of requested parameter
   */
  public final function __get($property) {
    return (isset($this->config->$property) ? $this->config->$property : null);
  }

  /**
   *  save config to xml file specified in ini 
   */
  public function saveConfig() {
    $configXml = new Zend_Config_Writer_Xml();
    $configXml->write($this->appConfig->app->config->paths, $this->config);
  }

  /**
   * Creates new config from template specified in ini file 
   */
  private function createConfig() {
    $configXml = new Zend_Config_Writer_Xml();
    $template = new Zend_Config($this->appConfig->app->config->template, true);
    $configXml->write($this->appConfig->app->config->paths, $template);
  }

  public function setTrans($key, $value) {
    $key = substr($key, 1);
    $keys = explode('-', $key);

    $confPath = $this->config;


    for ($index = 0; $index < count($keys); $index++) {
      if ($index == count($keys) - 1) {
        if (($confPath->$keys[$index] != $value) && (trim($confPath->$keys[$index]) != "")) {
          $confPath->$keys[$index] = $value;
          return "-" . $key;
        }
        $confPath->$keys[$index] = $value;
      }
      $confPath = $confPath->$keys[$index];
    }
  }

  /**
   * Returns filtered object - without locked flag
   * @param Zend_Config $config
   * @return Zend_Config 
   */
  public function getUnlocked($config = null) {
    if (!$config)
      $config = clone $this->config;
    foreach ($config as $key => $value) {
      if (is_object($value))
        $value = $this->getUnlocked($value);
      if (isset($value->locked))
        unset($config->$key);
    }
    return $config;
  }
  
  public function isOffline() {
  	if ((bool)$this->offline->power) {
	  	$fromDate = ($this->status->offline_from !== '') ? new DateTime($this->status->offline_from) : null;
	  	$toDate = ($this->status->offline_from !== '') ? new DateTime($this->status->offline_to) : null;
	  	$now = new DateTime('now');
	  	
	  	if (($toDate !== null && $fromDate !== null && $fromDate < $now && $now < $toDate) || ($toDate !== null && $now < $toDate) || ($fromDate !== null && $now > $fromDate))
	  		return true;
	  	else 
	  		return false;
  	}
  }

}

?>